import tensorflow as tf

a = tf.random.normal([4, 4])
print(a)

mask = a > 0
print(mask)

# 使用boolean_mask 得到数值
value = tf.boolean_mask(a, mask)
print(value)

# 使用坐标方法(where & gather_nd)获得数值
# 获取坐标
indices = tf.where(mask)
print(indices)
# 根据坐标获取对应数值
value = tf.gather_nd(a, indices)
print(value)

# 从两个矩阵中采用
# 第一个采样true，第二个采用false的值
aa = tf.ones([4, 4])
bb = tf.zeros([4, 4])
c = tf.where(mask, aa, bb)
print(c)

# 根据指定坐标和指定值填入一个全0的tensor
indices = tf.constant([[4], [3], [1], [7]])
updates = tf.constant([9, 10, 11, 12])
shape = tf.constant([8])
scatter = tf.scatter_nd(indices, updates, shape)
print(scatter)

# Broadcasts parameters for evaluation on an N-D grid
# 将两个坐标放在两个tensor中
x = tf.linspace(-2., 2., 5)
y = tf.linspace(-2., 2., 6)
points_x, points_y = tf.meshgrid(x, y)
print('x:\n', x)
print('y:\n', y)
print('points_x:\n', points_x)
print('points_y:\n', points_y)

point = tf.stack([points_x, points_y], axis=2)
print('point:\n', point)
